package com.itheima.youdianda.controller;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.YddAdvposDto;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class YddAdvposController {
    @Autowired
    private YddAdvposService yddAdvposService;
   @GetMapping("/list")
    public R<PageDto> page(@RequestHeader("x-token") String token, int page, int limit, String advposname,String advposdesc){
       boolean flag = JwtUtils.verifyToken(token);
       if (flag) {
           PageDto<YddAdvposDto> pageInfo = yddAdvposService.pagede( page, limit, advposname, advposdesc);
           return R.success(pageInfo);
       }
       return R.success(null,"查询失败");
   }
        @PostMapping("/add")
        public R<String> save(@RequestHeader("x-token") String token,@RequestBody YddAdvpos yddAdvpos){
            boolean flag = JwtUtils.verifyToken(token);
            if (flag) {
                yddAdvposService.save(yddAdvpos);
                return R.success("新增信息成功");
            }
            return R.success("新增信息失败");
        }
    @PostMapping("/update")
    public R<String> update(@RequestHeader("x-token") String token,@RequestBody YddAdvpos yddAdvpos){
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {

            yddAdvposService.updateById(yddAdvpos);
        }
        return R.success("修改信息成功");
    }
    @GetMapping ("/all")
    public R<List<YddAdvpos>> select(@RequestHeader("x-token") String token){
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            List<YddAdvpos> findall = yddAdvposService.findall();
            return R.success(findall);
        }

        return null;
    }
    @GetMapping ("/rowInfo")
    public R<YddAdvpos> xingqing(@RequestHeader("x-token") String token, String id){
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            YddAdvpos byId = yddAdvposService.getById(id);
            return R.success(byId);
        }
        return R.error("查不到数据");
    }
    @GetMapping("/del")
    public R<String> delete(@RequestHeader("x-token") String token,String id){
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            yddAdvposService.remove(id);
        }
        return R.success("分类信息删除成功");
    }
    }

